package com.brkj.course;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import com.brkj.course.model.DS_CourseClassInfo;
import com.brkj.main.R;
import com.brkj.model.DS_CourseClass;
import com.dgl.sdk.util.Utils;

import java.util.ArrayList;
import java.util.List;

public class CourseClassInfoAdapter extends BaseAdapter{
	private ListItemView listItemView;
	protected LayoutInflater listContainer;
	private Context context;
    private CourseViewClass courseViewClass;
	private List<DS_CourseClassInfo> courseClassInfoList;
	private List<DS_CourseClassInfo> courseClassInfoOne = new ArrayList<DS_CourseClassInfo>();

	public CourseClassInfoAdapter(Context context, List<DS_CourseClassInfo> courseClassInfoList, CourseViewClass courseViewClass){
		this.context = context;
		this.courseClassInfoList = courseClassInfoList;
        this.courseViewClass = courseViewClass;


        for(DS_CourseClassInfo info:courseClassInfoList){
            if(info.getParentID() == 0){
                courseClassInfoOne.add(info);
            }
        }

		listContainer = LayoutInflater.from(context);
	}
	
	private static class ListItemView{
		public TextView courseClassName;
		public TextView courseNum;
		public GridView gridView;
	};
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return courseClassInfoOne.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// 自定义视图
		listItemView = new ListItemView();
		// 获取list_item布局文件的视图
		if (convertView == null) {
			convertView = listContainer.inflate(R.layout.course_class_info_item, null);
			// 获取控件对象
			listItemView.courseClassName = (TextView) convertView.findViewById(R.id.name);
			listItemView.courseNum = (TextView) convertView.findViewById(R.id.courseNum);
			listItemView.gridView = (GridView) convertView.findViewById(R.id.gridview);

			// 设置控件集到convertView
			convertView.setTag(listItemView);
		} else {
			listItemView = (ListItemView) convertView.getTag();
		}

        listItemView.courseClassName.setText(courseClassInfoOne.get(position).getName());
        listItemView.courseNum.setText("共" + courseClassInfoOne.get(position).getCourseNum() + "课");

        List<DS_CourseClassInfo> courseClassInfoSecond = new ArrayList<DS_CourseClassInfo>();
        for(DS_CourseClassInfo info: courseClassInfoList){
            if(info.getParentID() == courseClassInfoOne.get(position).getClassId()){
                courseClassInfoSecond.add(info);
            }

        }

        CourseClassSecondAdapter adapter = new CourseClassSecondAdapter(context,
                courseClassInfoSecond, courseViewClass);
        listItemView.gridView.setAdapter(adapter);

		return convertView;
	}
	
}
